﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Text;
using System.Collections;
using GreatB2C.Utility;

namespace GreatB2C.Web.Json.Common
{
    public partial class Common : System.Web.UI.Page
    {
        /// <summary>
        /// Json 数据集合
        /// </summary>
        protected string strJson = "";

        /// <summary>
        /// 模板存放相对路径
        /// </summary>
        protected const string TemplatePath = "/Template";

        /// <summary>
        /// 模板目录下待过滤的目录列表
        /// </summary>
        protected const string Filter = "标签库,分页标签库";

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                if ( Request.QueryString[ "action" ] != null && Request.QueryString[ "action" ].ToString( ).Trim( ) != "" )
                {
                    string action = Request.QueryString[ "action" ].ToString( ).Trim( );

                    if ( action.Trim( ).ToLower( ) == "templatelist" )
                    {
                        GetTemplateList( );
                    }
                    else if ( action.Trim( ).ToLower( ) == "filelist" )
                    {
                        GetFileList( );
                    }
                }
            }
        }

        protected void GetTemplateList( )
        {
            string ctlid = "";
            if ( Request.QueryString[ "ctl" ] != null && Request.QueryString[ "ctl" ].ToString( ) != "" )
            {
                ctlid = Request.QueryString[ "ctl" ].ToString( ) + "_";
            }

            ArrayList aryFilter = new ArrayList( Filter.Split( ',' ) );
            StringBuilder sbHtml = new StringBuilder( );
            DirectoryInfo root = new DirectoryInfo( Server.MapPath( TemplatePath ) );
            DirectoryInfo[] dirs = root.GetDirectories( );
            if ( dirs.Length > 0 )
            {
                //sbHtml.Append( "<ul id=\"dir_" + this.ClientID + "\" class=\"easyui-tree\" animate=\"true\" dnd=\"true\">" + DataDefine.NewLine );
                foreach ( DirectoryInfo dir in dirs )
                {
                    if ( Filter.Contains( dir.Name ) ) continue;

                    sbHtml.Append( "<li iconCls=\"icon-tree\"><span><a href=\"javascript:void(0);\" onclick=\"" + ctlid + "loadfile('/" + dir.Name + "');\">" + dir.Name + "</a></span>" + DataDefine.NewLine );
                    LoadDir( dir , aryFilter , ctlid , ref sbHtml );
                    sbHtml.Append( "</li>" + DataDefine.NewLine );
                }
                //sbHtml.Append( "</ul>" + DataDefine.NewLine );
            }

            strJson = sbHtml.ToString( );
        }

        protected void LoadDir( DirectoryInfo parentDir , ArrayList aryFilter , string controlid , ref StringBuilder sbHtml )
        {
            DirectoryInfo[] dirs = parentDir.GetDirectories( );
            if ( dirs.Length > 0 )
            {
                sbHtml.Append( "<ul>" + DataDefine.NewLine );

                foreach ( DirectoryInfo dir in dirs )
                {
                    if ( Filter.Contains( dir.Name ) ) continue;

                    string relatepath = dir.FullName;
                    relatepath = relatepath.Replace( "\\" , "/" ).ToLower( );
                    relatepath = relatepath.Substring( relatepath.IndexOf( TemplatePath.ToLower( ) ) + TemplatePath.Length );

                    sbHtml.Append( "<li iconCls=\"icon-dir\"><span><a href=\"javascript:void(0);\" onclick=\"" + controlid + "loadfile('/" + relatepath + "');\" >" + dir.Name + "</a></span>" + DataDefine.NewLine );

                    LoadDir( dir , aryFilter , controlid , ref sbHtml );
                }
                sbHtml.Append( "</ul>" + DataDefine.NewLine );
            }
        }

        protected void GetFileList( )
        {
            string ctlid = "";
            if ( Request.QueryString[ "ctl" ] != null && Request.QueryString[ "ctl" ].ToString( ) != "" )
            {
                ctlid = Request.QueryString[ "ctl" ].ToString( ) + "_";
            }

            string relatepath = TemplatePath;
            if ( Request.QueryString[ "path" ] != null && Request.QueryString[ "path" ].ToString( ) != "" )
            {
                relatepath = TemplatePath + Request.QueryString[ "path" ].ToString( );
            }

            ArrayList aryFilter = new ArrayList( Filter.Split( ',' ) );
            StringBuilder sbHtml = new StringBuilder( );
            DirectoryInfo root = new DirectoryInfo( Server.MapPath( relatepath ) );
            DirectoryInfo[] dirs = root.GetDirectories( );
            if ( dirs.Length > 0 )
            {
                foreach ( DirectoryInfo dir in dirs )
                {
                    if ( Filter.Contains( dir.Name ) ) continue;

                    string tmppath = dir.FullName;
                    tmppath = tmppath.Replace( "\\" , "/" ).ToLower( );
                    tmppath = tmppath.Substring( tmppath.IndexOf( TemplatePath.ToLower( ) ) + TemplatePath.Length );

                    sbHtml.Append( "<li class='icon_dir'><span><a href=\"javascript:void(0);\" ondblclick=\"" + ctlid + "loadfile('/" + tmppath + "');\" >" + dir.Name + "</a></span></li>" + DataDefine.NewLine );
                }
            }

            FileInfo[] files = root.GetFiles( );
            if ( files.Length > 0 )
            {
                foreach ( FileInfo file in files )
                {
                    string tmppath = file.FullName;
                    tmppath = tmppath.Replace( "\\" , "/" ).ToLower( );
                    tmppath = tmppath.Substring( tmppath.IndexOf( TemplatePath.ToLower( ) ) + TemplatePath.Length );

                    sbHtml.Append( "<li class='icon_doc'><span><a href=\"javascript:void(0);\" onclick=\"" + ctlid + "choose('" + tmppath + "');\"  ondblclick=\"" + ctlid + "choosed('" + tmppath + "');\">" + file.Name + "</a></span></li>" + DataDefine.NewLine );
                }
            }

            strJson = sbHtml.ToString( );
        }
    }
}
